﻿using UnityEngine;
using System.Collections;

public class LevelPanelManager : MonoBehaviour {
    public static LevelPanelManager instance;
    public GameObject TypePanel;
    public GameObject LevelItemPanel;
    public GameObject typeItem;
    public GameObject levelItem;
    public string UpdateLevelItemName;
	// Use this for initialization
	void Start () {
        instance = this;
        InitType();
        InitLevelItem();
        AllCount();
	}
    public void AllCount()
    {
        string dta = "射箭、拳击、击剑、皮滑艇、蹦床、柔道、赛艇、游泳（蹼泳）、救生（含水上救生）、射击、摔跤、帆船（帆板）、跳伞、动力伞、牵引伞、滑翔（滑翔伞、滑翔机、悬挂滑翔）、热气球、登山、攀岩、蹦极、探险、汽车、摩托车、摩托艇、弓弩、滑水、武术（含散打）、跆拳道、健美、健美操、体育舞蹈、漂流、冲浪、潜水、飞艇、轻型飞机、运动游艇、空手道、舍宾运动、赛马、高尔夫球、足球、篮球、排球、羽毛球、乒乓球、网球、软式网球、橄榄球、棒球、垒球、高尔夫球、马球、手球、水球、保龄球、门球、藤球、冰球、台球、地掷球、曲棍球、毽球、板球、羽毛球、棒球、篮球、足球、手球、曲棍球、垒球、乒乓球、网球（含软式网球）、排球（含沙滩排球）、地掷球、藤球、门球、毽球、壁球、台球、高尔夫球、保龄球、沙壶球、橄榄球；体操（含艺术体操）、蹦床、技巧、健美、健美操、体育舞蹈、木兰拳（含木兰剑、木兰扇等）、舍宾运动、运动保健按摩；花样滑冰、冰上舞蹈、冰球、速度滑冰、滑雪；田径、越野赛、滑板、轮滑、自行车（含山地车、独轮车等）、滑水、塞艇、皮滑艇、救生（含水上救生）；赛马、举重、击剑、拳击、柔道、摔跤、武术（含散打）、跆拳道、空手道、健身气功；国际象棋、中国象棋、围棋、桥牌；钓鱼、信鸽、舞龙、舞狮、龙舟、风筝、拔河；飞镖、秋千、斗鸡、斗牛；航海模型、航空模型、车辆模型、航天模型、无线电、定向运动、跳伞、动力伞、牵引伞、滑翔、跳伞、动力伞、牵引伞、滑翔";
        string[] data= dta.Split( '、' );
        Debug.Log( "数量数量数量数量数量:" + data.Length );
    }
	// Update is called once per frame
	void Update () {
	
	}
    public void InitType()
    {
        
        for( int i = 0; i < 14; i++ )
        {
            GameObject go = Instantiate( typeItem );
            int y = i / 3;
            int x = i % 3;
            go.transform.parent = TypePanel.transform;
            go.transform.localPosition = new Vector3( -210 + x * 210, 380 - y * 204 );
            go.transform.localScale = new Vector3( 1, 1 );
            go.transform.name = "TypeItem" + i;
            //go.transform.Find("Lock").gameObject.GetComponent<UISprite>().spriteName = i.ToString();
            go.transform.GetComponent<UISprite>().spriteName = i.ToString();
            //设置锁
            if( PlayerPrefs.GetInt( "id" ) >= i * 24 )  //24表示一个类型的小关卡数
            {
                go.transform.Find( "Lock" ).gameObject.SetActive( false );
                go.gameObject.GetComponent<UISprite>().enabled = true;
            }
           
        }
    }
    public void InitLevelItem()
    {
        for (int k = 0; k < 9; k++)
        {
            GameObject obj = Instantiate(LevelItemPanel);
            obj.transform.parent = TypePanel.transform;
            obj.transform.localPosition = new Vector3(0, 0);
            obj.transform.localScale = new Vector3(1, 1);
            obj.transform.name = "LevelItemPanel" + k;
            obj.transform.gameObject.SetActive(false);
            for (int i = 0; i < 24; i++)
            {
                GameObject go = Instantiate(levelItem);
                int y = i / 4;
                int x = i % 4;
                go.transform.parent = obj.transform;
                go.transform.localPosition = new Vector3(-223 + x * 150, 400 - y * 150);
                go.transform.localScale = new Vector3(1, 1);
                go.transform.name = "LevelItem" + i;
                go.transform.Find("Label").GetComponent<UILabel>().text = (i + 1).ToString();
                //初始化levelItem的锁
                if( PlayerPrefs.GetInt( "id" ) > k * 24 + i )
                {
                    go.transform.Find( "Lock" ).gameObject.SetActive( false );
                    go.transform.gameObject.GetComponent<UISprite>().enabled = true;
                }                
            }
        }
       
    }
    //刷新TypeItem的锁
    public void UpdateTypeLock()
    {
        for( int i = 0; i < 9; i++ )
        {
            string TypeItem = "TypeItem" + i+"/Lock";
            string lujin = "TypeItem" + i;
            if( PlayerPrefs.GetInt( "id" ) >= i * 24 )
            {
                TypePanel.transform.Find( TypeItem ).gameObject.SetActive( false );
                TypePanel.transform.Find( lujin ).gameObject.GetComponent<UISprite>().enabled = true;
            }           
        }
    }
    //刷新LevelItem的锁
    public void UpdateLevelLock(string name)
    {
        int k = int.Parse( name.Substring( name.Length - 1, 1 ));
        for( int i = 0; i < 24; i++ )
        {
            string LevelItem = name + "/LevelItem" + i + "/Lock";
            string lujin = name + "/LevelItem" + i;
            if( PlayerPrefs.GetInt( "id" ) > k * 24 + i )
            {               
                TypePanel.transform.Find( LevelItem ).gameObject.SetActive( false );
                TypePanel.transform.Find( lujin ).gameObject.GetComponent<UISprite>().enabled = true;
               
            }
        }
    }
    public void TypeStartOnClick(string type)
    {
        if( type.Length == 9 )
        {
            type = type.Substring( type.Length - 1, 1 );
        }
        else
        {
            type = type.Substring( type.Length - 2, 2 );

        }
        string NoLevelItemPanel = "LevelItemPanel" + type;
        TypePanel.transform.Find( NoLevelItemPanel ).gameObject.SetActive( true );
        UpdateLevelLock( NoLevelItemPanel );
        UpdateLevelItemName = NoLevelItemPanel;
    }
}
